---
title: "‎"
categories:
- java
- shiro
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orgdb14bb9">AdviceFilter : OncePerRequestFilter</a>
<ul>
<li><a href="#orgf675a3e">doFilterInternal</a></li>
<li><a href="#org12ad140">executeChain</a></li>
<li><a href="#org5c414c8">preHandle(ServletRequest, ServletResponse)</a></li>
<li><a href="#org819c769">postHandle</a></li>
<li><a href="#orgd8b69ea">cleanup</a></li>
<li><a href="#org15d1d62">afterCompletion</a></li>
</ul>
</li>
<li><a href="#org4c0d9c5">PathMatchingFilter : AdviceFilter</a>
<ul>
<li><a href="#org8992482">override preHandle(SevletRequest, ServletResponse)</a></li>
<li><a href="#org9bbc89f">onPreHandle(ServletRequest, ServletResponse, Object)</a></li>
</ul>
</li>
<li><a href="#org759c2eb">AccessControlFilter : PathMatchingFilter</a>
<ul>
<li><a href="#orgc1851ff">override onPreHandle(ServletRequest, ServletResponse, Object)</a></li>
</ul>
</li>
<li><a href="#org792436e">AuthenticationFilter : AccessControlFilter</a>
<ul>
<li><a href="#orge16df14">override isAccessAllowed(ServletRequest, ServletResponse, Object)</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="outline-2" id="outline-container-orgdb14bb9">
<h2 id="orgdb14bb9">AdviceFilter : OncePerRequestFilter</h2>
<div class="outline-text-2" id="text-orgdb14bb9">
</div>
<div class="outline-3" id="outline-container-orgf675a3e">
<h3 id="orgf675a3e">doFilterInternal</h3>
<div class="outline-text-3" id="text-orgf675a3e">
<div class="org-src-container">
<pre class="src src-java"><span style="font-weight: bold;">try</span> {

    <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">在executeChain之前执行 preHandle 方法</span>
    <span style="font-weight: bold; text-decoration: underline;">boolean</span> <span style="font-weight: bold; font-style: italic;">continueChain</span> = preHandle(request, response);
    <span style="font-weight: bold;">if</span> (log.isTraceEnabled()) {
        log.trace(<span style="font-style: italic;">"Invoked preHandle method.  Continuing chain?: ["</span> + continueChain + <span style="font-style: italic;">"]"</span>);
    }

    <span style="font-weight: bold;">if</span> (continueChain) {
        <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">调用了本类中的executeChain方法，方法如下</span>
        executeChain(request, response, chain);
    }
    <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">在executeChain之后执行 postHandle 方法</span>
    postHandle(request, response);
    <span style="font-weight: bold;">if</span> (log.isTraceEnabled()) {
        log.trace(<span style="font-style: italic;">"Successfully invoked postHandle method"</span>);
    }

} <span style="font-weight: bold;">catch</span> (<span style="font-weight: bold; text-decoration: underline;">Exception</span> <span style="font-weight: bold; font-style: italic;">e</span>) {
    exception = e;
} <span style="font-weight: bold;">finally</span> {
    <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">过程结束之后执行clean</span>
    cleanup(request, response, exception);
}
</pre>
</div>
</div>
</div>
<div class="outline-3" id="outline-container-org12ad140">
<h3 id="org12ad140">executeChain</h3>
<div class="outline-text-3" id="text-org12ad140">
<div class="org-src-container">
<pre class="src src-java">chain.doFilter(<span style="font-weight: bold; text-decoration: underline;">request</span>, <span style="font-weight: bold; text-decoration: underline;">response</span>);
</pre>
</div>
</div>
</div>
<div class="outline-3" id="outline-container-org5c414c8">
<h3 id="org5c414c8">preHandle(ServletRequest, ServletResponse)</h3>
<div class="outline-text-3" id="text-org5c414c8">
<p>
只是简单地返回true
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org819c769">
<h3 id="org819c769">postHandle</h3>
<div class="outline-text-3" id="text-org819c769">
<p>
空方法
</p>
</div>
</div>
<div class="outline-3" id="outline-container-orgd8b69ea">
<h3 id="orgd8b69ea">cleanup</h3>
<div class="outline-text-3" id="text-orgd8b69ea">
<p>
调用了afterCompletion方法
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org15d1d62">
<h3 id="org15d1d62">afterCompletion</h3>
<div class="outline-text-3" id="text-org15d1d62">
<p>
空方法
</p>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-org4c0d9c5">
<h2 id="org4c0d9c5">PathMatchingFilter : AdviceFilter</h2>
<div class="outline-text-2" id="text-org4c0d9c5">
<p>
abstract class
提供：只处理特定的url，而对其他的url不处理的功能
</p>
</div>
<div class="outline-3" id="outline-container-org8992482">
<h3 id="org8992482">override preHandle(SevletRequest, ServletResponse)</h3>
<div class="outline-text-3" id="text-org8992482">
<p>
重写的这个方法，判断某个请求的URL是否需要处理，并提供了另外一个方法，供子类自定义
</p>
</div>
</div>
<div class="outline-3" id="outline-container-org9bbc89f">
<h3 id="org9bbc89f">onPreHandle(ServletRequest, ServletResponse, Object)</h3>
<div class="outline-text-3" id="text-org9bbc89f">
<ul class="org-ul">
<li>第三个参数，mappedValue 这个参数的值是配置在这个filter中的，the filter-specific config value mapped to this filter in the URL rules mappings.</li>
</ul>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-org759c2eb">
<h2 id="org759c2eb">AccessControlFilter : PathMatchingFilter</h2>
<div class="outline-text-2" id="text-org759c2eb">
<p>
提供：资源权限控制功能，当用户没有授权时，重定向到登录页
登录页的链接，通过类方法setLoginUrl设置
</p>
</div>
<div class="outline-3" id="outline-container-orgc1851ff">
<h3 id="orgc1851ff">override onPreHandle(ServletRequest, ServletResponse, Object)</h3>
<div class="outline-text-3" id="text-orgc1851ff">
<p>
调用了isAccessAllowed和onAccessDenied，这两个方法被定义在本类中，但是它们是空方法。
</p>
</div>
</div>
</div>
<div class="outline-2" id="outline-container-org792436e">
<h2 id="org792436e">AuthenticationFilter : AccessControlFilter</h2>
<div class="outline-text-2" id="text-org792436e">
</div>
<div class="outline-3" id="outline-container-orge16df14">
<h3 id="orge16df14">override isAccessAllowed(ServletRequest, ServletResponse, Object)</h3>
<div class="outline-text-3" id="text-orge16df14">
<div class="org-src-container">
<pre class="src src-java"><span style="font-weight: bold;">protected</span> <span style="font-weight: bold; text-decoration: underline;">boolean</span> <span style="font-weight: bold;">isAccessAllowed</span>(<span style="font-weight: bold; text-decoration: underline;">ServletRequest</span> <span style="font-weight: bold; font-style: italic;">request</span>, <span style="font-weight: bold; text-decoration: underline;">ServletResponse</span> <span style="font-weight: bold; font-style: italic;">response</span>, <span style="font-weight: bold; text-decoration: underline;">Object</span> <span style="font-weight: bold; font-style: italic;">mappedValue</span>) {
    <span style="font-weight: bold; font-style: italic;">// </span><span style="font-weight: bold; font-style: italic;">熟悉的 getSubject 方法，终于开始shiro了</span>
    <span style="font-weight: bold; text-decoration: underline;">Subject</span> <span style="font-weight: bold; font-style: italic;">subject</span> = getSubject(request, response);
    <span style="font-weight: bold;">return</span> subject.isAuthenticated();
}
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="author">Author: amo</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
